package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.zzyl.dto.BedDto;
import com.zzyl.entity.Bed;
import com.zzyl.enums.BasicEnum;
import com.zzyl.exception.BaseException;
import com.zzyl.mapper.BedMapper;
import com.zzyl.service.BedService;
import com.zzyl.vo.BedVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class BedServiceImpl implements BedService {

    @Autowired
    private BedMapper bedMapper;

    @Override
    public List<BedVo> getBedsByRoomId(Long roomId) {
        return bedMapper.getBedsByRoomId(roomId);
    }

    @Override
    public void addBed(BedDto bedDto) {

        //Bed bed = new Bed();
        //bed.setBedNumber(bedDto.getBedNumber());
        Bed bed = BeanUtil.toBean(bedDto, Bed.class);

        // 不全 其他属性
        bed.setCreateTime(LocalDateTime.now());
        bed.setCreateBy(1L);
        bed.setBedStatus(0);
        // 调用 mapper 实现数据保存
        try{
            bedMapper.addBed(bed);
        }catch (Exception e){
            throw new BaseException(BasicEnum.BED_INSERT_FAIL);
        }


    }

    @Override
    public BedVo getBedById(Long id) {
       Bed bed= bedMapper.getBedById(id);
        BedVo bedVo = BeanUtil.toBean(bed, BedVo.class);
        return bedVo;
    }

    @Override
    public void updateBed(BedDto bedDto) {
        Bed bed = BeanUtil.toBean(bedDto, Bed.class);

        // 补全字段
        bed.setUpdateBy(1L);
        bed.setUpdateTime(LocalDateTime.now());
        // 调用mapper 实现bed数据更新
        bedMapper.updateBed(bed);
    }

    @Override
    public void deleteById(Long id) {

        bedMapper.deleteById(id);
    }

}

